﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using T.FAS.Runtime.Base.Common;
using T.FAS.Runtime.Metadata.Entity.Page;
using T.ServiceComponent.DAC.Service;
using T.ServiceComponent.Database.Service;

namespace T.FAS.Runtime.Page.Dao
{
    public class ThemeMetaDataDao : DaoService<ThemeMetaData>
    {
        public ThemeMetaDataDao() :
           base()
        {
            DBName = "TFAS";
        }

        public override string TableName => "FAS_ThemeMetaData";
        public override string IDColumnName => "ID";

        public override List<string> ExceptColumnName => new List<string> { "MetaDataID", "ThemeURL", "DefaultThemeURL", "MetaDataCode", "MetaDataName", "MetaData", "CurrentVersion", "CompatibleMinVersion", "EngineVersion", "ProjectBranchID" };
        public override Dictionary<string, object> GetDataMapping(ThemeMetaData data)
        {
            var datas = base.GetDataMapping(data);
            datas.Remove("ClientType");
            datas.Add("ClientType", (int)(data.ClientType));
            return datas;
        }
        public override ThemeMetaData ConvertToObject(Dictionary<string, PropertyInfo> columns, DataRow row)
        {
            var myCol = new Dictionary<string, PropertyInfo>(base.Columns);
            myCol.Remove("ClientType");
            var data = base.ConvertToObject(myCol, row);
            if (row["ClientType"] != DBNull.Value)
                data.ClientType = (ClientType)Enum.Parse(typeof(ClientType),
                                                Convert.ToString(row["ClientType"]));
            return data;
        }
        public List<ThemeMetaData> GetDefaultTheme()
        {
            var result = new List<ThemeMetaData>();
            List<object> objects = base.GetObjectsByFilter($"IsDefault = 1 and TenantID = '{UserUtil.GetCurrentUserTenantId()}'");
            if (objects != null)
            {
                objects.ForEach(obj => result.Add(obj as ThemeMetaData));
            }
            return result;
        }
    }
}
